package ru.susu.algebra.centralunits.alternating.initializers;

import java.util.LinkedHashMap;
import java.util.List;

import ru.susu.algebra.centralunits.CentralUnitsOptions;
import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.chartable.constructor.AlternatingCharTableUtil;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;

/**
 * @author akargapolov
 * @since: 26.08.2010
 */
public class SpecialRowsInitializer extends MathMethodWithInitializers<Object>
{
	private static final String SPECIAL_ROWS = "SpecialRows";
	private static final String SPECIAL_NOT_INTEGER_CHARACTER_ROWS = "SpecialNotIntegerCharacterRows";
	private static final String SPECIAL_ROWS_INDEX = "SpecialRowsIndex";

	@Override
	protected Object directRun(IPropertySource ps) throws Exception
	{
		int number = (Integer)ps.getValue(CentralUnitsOptions.NUMBER);

		if (ps.getValue(SPECIAL_ROWS) == null)
			ps.setValue(SPECIAL_ROWS, AlternatingCharTableUtil.listSpecialRows(number));

		if (ps.getValue(SPECIAL_NOT_INTEGER_CHARACTER_ROWS) == null)
			ps.setValue(SPECIAL_NOT_INTEGER_CHARACTER_ROWS, AlternatingCharTableUtil.listSpecialRowsNotIntegerCharacter(number));

		if (ps.getValue(SPECIAL_ROWS_INDEX) == null)
			ps.setValue(SPECIAL_ROWS_INDEX, AlternatingCharTableUtil.indexRowNumbers(listSpecialRows(ps)));

		return null;
	}

	@SuppressWarnings("unchecked")
	public static List<Partition> listSpecialRows(IPropertySource ps)
	{
		return (List<Partition>)ps.getValue(SPECIAL_ROWS);
	}

	@SuppressWarnings("unchecked")
	public static List<Partition> listSpecialNotIntegerCharacterRows(IPropertySource ps)
	{
		return (List<Partition>)ps.getValue(SPECIAL_NOT_INTEGER_CHARACTER_ROWS);
	}

	@SuppressWarnings("unchecked")
	public static LinkedHashMap<Partition, Integer> getSpecialRowsIndex(IPropertySource ps)
	{
		return (LinkedHashMap<Partition, Integer>)ps.getValue(SPECIAL_ROWS_INDEX);
	}

	public static String getIndexByRowNumber(IPropertySource ps, Partition partition)
	{
		return  "_{" + getRowNumber(ps, partition) + "}";
	}

	public static Integer getRowNumber(IPropertySource ps, Partition partition)
	{
		return getSpecialRowsIndex(ps).get(partition);
	}

	public static boolean isSpecialNotIntegerRow(Partition p, IPropertySource ps)
	{
		return listSpecialNotIntegerCharacterRows(ps).contains(p);
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return new Class[0];
	}

}
